//
//  FGRecordWaveView.m
//  VocieRecorder
//
//  Created by Fengur on 16/9/1.
//  Copyright © 2016年 code.uworks.fengur. All rights reserved.
//

#import "FGRecordWaveView.h"
#import "WVDWaveView.h"

@interface FGRecordWaveView ()<WaveViewDelegate> {
    CGFloat _pointsXValue;
    NSInteger _tagCount;
    CGFloat _lastValue;
}
@end

@implementation FGRecordWaveView

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupControls];
    }
    return self;
}

- (void)setupControls {
    _tagCount = 0;
    self.backgroundColor = [UIColor ghostWhite];
    _tagPointsValueArray = [NSMutableArray new];
    _baseScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    [self addSubview:_baseScrollView];
    _baseScrollView.contentSize = CGSizeMake(ScreenWidth*100, self.height);
    _baseScrollView.showsHorizontalScrollIndicator = NO;
    _baseScrollView.scrollEnabled = NO;

    _waveView = [[WVDWaveView alloc]
        initWaveWithType:kWaveTypeMirror
                   Frame:CGRectMake(0, self.height / 4, ScreenWidth*100, self.height / 2)
                MaxValue:0
                MinValue:-50];
    [_waveView setZeroPointValue:-40];
    [_waveView setZeroPointLineWidth:2];
    [_waveView setWaveLineWidth:2.5];
    _waveView.backgroundColor = [UIColor clearColor];
    _waveView.delegate = self;
    [_baseScrollView addSubview:_waveView];

    UIView *partLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _waveView.width, 1.7)];
    partLineView.backgroundColor = [UIColor blueColor];
    partLineView.centerY = self.height / 2;
    [self addSubview:partLineView];
}

- (void)updateWaveValueWithVoiceValue:(float)voiceValue {
    [_waveView startWavingWithValue:voiceValue];
}

- (void)getCurrentValueX:(CGFloat)valueX {
    _pointsXValue = valueX;
}

- (void)makePoints {
    _tagCount = _tagCount + 1;
    UILabel *pointsView =
        [[UILabel alloc] initWithFrame:CGRectMake(_pointsXValue, 0, PADDING_CELL * 2,
                                                  PADDING_CELL * 2)];
    pointsView.layer.cornerRadius = SPadding * 2;
    pointsView.layer.masksToBounds = YES;
    pointsView.text = [NSString stringWithFormat:@"%ld", _tagCount];
    pointsView.textColor = [UIColor whiteColor];
    pointsView.textAlignment = NSTextAlignmentCenter;
    pointsView.font = AvenirLightFont(8);

    pointsView.backgroundColor = [UIColor blackColor];
    [_waveView addSubview:pointsView];

    [_tagPointsValueArray addObject:[NSString stringWithFormat:@"%f", _pointsXValue]];
}

@end
